package com.ems.controller;

import com.ems.entity.TMachine;
import com.ems.service.ITMachineService;
import com.ems.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author CSC
 * @since 2022-06-28
 */
@RestController
@RequestMapping("/t-machine")
public class TMachineController {
    @Autowired
    ITMachineService itMachineService;

    @GetMapping("/getlist")
public ResultUtil getListMachine(){
        List<TMachine> list=itMachineService.getListMachine();
        if(list!=null)
        {
            return ResultUtil.success(list);
        }
        else {
            return ResultUtil.fail("获取失败");
        }
    }
    @PostMapping("/add")
    public ResultUtil addMachine(@RequestBody TMachine tMachine ){
        int num=itMachineService.addMachine(tMachine);
        if(num>0){
            return ResultUtil.success("添加成功");
        }
        else {
            return  ResultUtil.fail("添加失败");
        }
    }
    @PostMapping("/delete")
    public ResultUtil deleteMachine(Integer id){
        int num=itMachineService.deleteMachine(id);
        if(num>0){
            return ResultUtil.success(num);
        }
        else{return ResultUtil.fail("删除失败");}
        }
    @PostMapping("/update")
    public ResultUtil updateMachine(TMachine tMachine){
        int num=itMachineService.updateMachine(tMachine);
        if(num>0){
            return ResultUtil.success("修改成功");}
            else
                {return ResultUtil.fail("修改失败");}
        }
    @GetMapping("/state")
    public ResultUtil getStateById(Integer id){
        return ResultUtil.success(itMachineService.getStateById(id));
    }

    @GetMapping("/temp")
    public ResultUtil getTempList(){
        return ResultUtil.success(itMachineService.getTempList());
    }
    @GetMapping("/pressure")
    public ResultUtil getPressureList(){
        return ResultUtil.success(itMachineService.getPressureList());
    }


    //基础部分
    //增删改查部分
    @GetMapping("/list")
    public ResultUtil list(){
        if (itMachineService.list()!=null){
            return ResultUtil.success(itMachineService.list());
        }
        else {
            return ResultUtil.fail("获取失败");
        }
    }
    //新增
    @PostMapping("/addMachine")
    public ResultUtil add(@RequestBody TMachine tMachine){
        if (itMachineService.save(tMachine)){
            return ResultUtil.success("添加成功");
        }
        else {
            return ResultUtil.fail("添加失败");
        }
    }
    //修改
    @PutMapping("/update")
    public ResultUtil update(@RequestBody TMachine tMachine){
        if (itMachineService.updateById(tMachine)){
            return ResultUtil.success("修改成功");
        }
        else {
            return ResultUtil.fail("修改失败");
        }
    }
    //逻辑删除
    @DeleteMapping("/removeByLogic/{idList}")
    public ResultUtil removeByLogic(@PathVariable("idList") Collection<Integer> idList){
        if (itMachineService.removeByIds(idList)){
            return ResultUtil.success("删除成功");
        }
        else {
            return ResultUtil.fail("删除失败");
        }
    }

    //业务部分
    //设备维护
    @PutMapping("/updateStatus/{pid}")
    public ResultUtil updateStatus(@PathVariable("pid") Integer id,@RequestBody TMachine tMachine){
        if (itMachineService.updateStatus(id,tMachine)){
            return ResultUtil.success("修改成功");
        }
        else {
            return ResultUtil.fail("修改失败");
        }

    }
    //设备报修
    @PutMapping("/repair/{pid}")
    public ResultUtil repair(@PathVariable("pid") Integer id,@RequestBody TMachine tMachine){
        if (itMachineService.repair(id,tMachine)){
            return ResultUtil.success("修改成功");
        }
        else {
            return ResultUtil.fail("修改失败");
        }

    }
    }



